﻿using D.Traceability.Domain;
using D.Traceability.ErrorCode;
using D.Traceability.Infrastructure.Infrastructure;
using D.Traceability.Write.API.APPlicant.Command;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace D.Traceability.Write.API.APPlicant.CommandHandler
{
    /// <summary>
    /// 溯源单个对象反填(通过 -- 生产批次查询)
    /// </summary>
    public class FanTianTraceCommandHandler : IRequestHandler<FanTianTraceCommand, APIResult<SU_TraceabilityModel>>
    {
        private readonly IBaseRepository<SU_TraceabilityModel> baseRepository;

        public FanTianTraceCommandHandler(IBaseRepository<SU_TraceabilityModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }


        /// <summary>
        /// 溯源单个对象反填(通过 -- 生产批次查询)
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<SU_TraceabilityModel>> Handle(FanTianTraceCommand request, CancellationToken cancellationToken)
        {
            APIResult<SU_TraceabilityModel> result = new APIResult<SU_TraceabilityModel>();
            var list = await baseRepository.GetAll().Result.FirstOrDefaultAsync(x => x.TraceaBatch == request.TraceaBatch);
            result.data = list;
            return await Task.FromResult(result);
        }
    }
}
